/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import ucm.*;

import java.sql.*;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

import security.SimpleMD5;

/**
 *
 * @author jc154639
 */
public class MySqlDAO extends MyDAO{
    private static final String databaseURL = "jdbc:mysql://dunk.it.jcu.edu.au/3120S109g08";
    private Connection db;
    private final String userName = "3120S109g08";
    private final String password = "e7d74ec6f6";
    private Statement stat;
    private String query;
    private ResultSet result;

    public static void init() {
        MyDAO.instance = new MySqlDAO();
    }

    private void connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();;
            db = DriverManager.getConnection(databaseURL, userName, password);
        } catch (SQLException e) {
            System.out.println("Database connection error: " + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        } catch (ClassNotFoundException e) {
            System.out.println("Error loading JDBC Driver: " + e.getMessage());
            System.exit(0);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    private void disconnect() {
        try {
            db.close();
            System.out.println("Database connection terminated");
        } catch (Exception e) { /* ignore close errors */ }

    }

    public User getUser(String username, String pwd) throws MyDAOException {
        User user = null;
        connect();
        try {
            stat = db.createStatement();
            query = "SELECT * FROM `users` WHERE `username` = '" + username + "'";
            result = stat.executeQuery(query);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            result.next();
            if(result.getString("password").equals(SimpleMD5.MD5(pwd))) {
                user = new User(
                        result.getString("f_name"),
                        result.getString("l_name"),
                        result.getString("userID"),
                        result.getString("home_address"),
                        result.getString("mail_address"),
                        result.getString("phone_num"),
                        result.getString("email_address"));
            }

        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState:     " + e.getSQLState());
            System.out.println("VendorError:  " + e.getErrorCode());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        disconnect();
        return user;
    }


    public Flight getFlight(String flightID) throws MyDAOException {
        Flight flight = null;
        connect();
        try {
            stat = db.createStatement();
            query = "SELECT * FROM `flights` WHERE `flightID` = '" + flightID + "'";
            result = stat.executeQuery(query);

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            result.next();

            flight = new Flight(
                    result.getString("flightID"),
                    result.getString("depart_loc"),
                    result.getString("depart_time"),
                    result.getString("arrival_loc"),
                    result.getString("arrival_time"),
                    result.getString("price"));

        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState:     " + e.getSQLState());
            System.out.println("VendorError:  " + e.getErrorCode());
        }

        disconnect();
        return flight;
    }

    public Admin getAdmin(String adminID) throws MyDAOException {
        connect();
        //Admin admin = new Admin();
        //disconnect();
        //return admin;
        return null;
    }
    public void createUser(String userName, String password, String firstName, String lastName, String homeAddress,
                           String mailingAddress, String contactPhone, String contactEmail)
            throws MyDAOException {
    }

    public void createManager(String managerName, String password) throws MyDAOException {

    }

    public static void main(String[] args) {
        MySqlDAO test = new MySqlDAO();

        try {
            if(test.getUser("test", "test") != null){
                System.out.println("User works!");
            }
            else System.out.println("User fails!");
        } catch (MyDAOException e) {
            e.printStackTrace();
        }
    }
}
